import { defineEventHandler, getQuery } from 'h3';

// 模拟SaaS应用数据
const mockApplications = [
  {
    id: '1',
    name: '智慧酒店管理系统',
    description: '基于SaaS的酒店管理解决方案',
    industryType: 'hotel',
    status: 'running',
    deployment: {
      environment: 'prod',
      region: 'cn-hangzhou',
      domain: 'hotel.example.com',
      version: '1.0.0',
      deployedAt: '2024-01-15T10:30:00Z',
      resources: {
        cpu: '4核',
        memory: '8GB',
        storage: '100GB',
      },
    },
    usage: {
      users: 150,
      storage: 1024,
      bandwidth: 512,
      lastActive: '2024-01-15T16:20:00Z',
      monthlyRevenue: 15000,
    },
    template: {
      id: 'hotel-premium',
      name: '酒店高级版',
      version: '1.0.0',
    },
    resources: [
      {
        id: '1',
        name: '酒店管理系统',
        type: 'service',
        status: 'active',
      },
      {
        id: 'payment-gateway',
        name: '支付网关',
        type: 'service',
        status: 'active',
      },
    ],
    createdAt: '2024-01-10T09:00:00Z',
    createdBy: 'admin',
    updatedAt: '2024-01-15T10:30:00Z',
  },
  {
    id: '2',
    name: '宠物店管理系统',
    description: '宠物店业务管理平台',
    industryType: 'pet',
    status: 'building',
    deployment: {
      environment: 'dev',
      region: 'cn-beijing',
      domain: '',
      version: '0.9.0',
      deployedAt: '',
      resources: {
        cpu: '2核',
        memory: '4GB',
        storage: '50GB',
      },
    },
    usage: {
      users: 0,
      storage: 0,
      bandwidth: 0,
      lastActive: '',
      monthlyRevenue: 0,
    },
    template: {
      id: 'pet-basic',
      name: '宠物基础版',
      version: '0.9.0',
    },
    resources: [
      {
        id: '2',
        name: '宠物店管理系统',
        type: 'service',
        status: 'building',
      },
    ],
    createdAt: '2024-01-12T14:30:00Z',
    createdBy: 'admin',
    updatedAt: '2024-01-15T16:20:00Z',
  },
  {
    id: '3',
    name: '教育管理系统',
    description: '教育机构综合管理平台',
    industryType: 'education',
    status: 'running',
    deployment: {
      environment: 'prod',
      region: 'cn-shanghai',
      domain: 'edu.example.com',
      version: '2.1.0',
      deployedAt: '2024-01-10T11:15:00Z',
      resources: {
        cpu: '8核',
        memory: '16GB',
        storage: '200GB',
      },
    },
    usage: {
      users: 500,
      storage: 2048,
      bandwidth: 1024,
      lastActive: '2024-01-15T14:30:00Z',
      monthlyRevenue: 50000,
    },
    template: {
      id: 'education-premium',
      name: '教育高级版',
      version: '2.1.0',
    },
    resources: [
      {
        id: '3',
        name: '教育管理系统',
        type: 'service',
        status: 'active',
      },
      {
        id: 'notification-service',
        name: '通知服务',
        type: 'service',
        status: 'active',
      },
    ],
    createdAt: '2023-12-01T00:00:00Z',
    createdBy: 'admin',
    updatedAt: '2024-01-10T11:15:00Z',
  },
  {
    id: '4',
    name: '零售POS系统',
    description: '零售店铺销售管理系统',
    industryType: 'retail',
    status: 'running',
    deployment: {
      environment: 'prod',
      region: 'cn-guangzhou',
      domain: 'retail.example.com',
      version: '1.5.0',
      deployedAt: '2024-01-08T14:45:00Z',
      resources: {
        cpu: '4核',
        memory: '8GB',
        storage: '100GB',
      },
    },
    usage: {
      users: 80,
      storage: 512,
      bandwidth: 256,
      lastActive: '2024-01-15T12:00:00Z',
      monthlyRevenue: 8000,
    },
    template: {
      id: 'retail-standard',
      name: '零售标准版',
      version: '1.5.0',
    },
    resources: [
      {
        id: '4',
        name: '零售POS系统',
        type: 'service',
        status: 'active',
      },
    ],
    createdAt: '2023-11-01T00:00:00Z',
    createdBy: 'admin',
    updatedAt: '2024-01-08T14:45:00Z',
  },
];

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const { page = 1, pageSize = 10, industryType, status, environment } = query;

  // 过滤数据
  let filteredData = [...mockApplications];

  if (industryType) {
    filteredData = filteredData.filter(
      (app) => app.industryType === industryType,
    );
  }

  if (status) {
    filteredData = filteredData.filter((app) => app.status === status);
  }

  if (environment) {
    filteredData = filteredData.filter(
      (app) => app.deployment.environment === environment,
    );
  }

  // 分页
  const pageNum = parseInt(page.toString());
  const size = parseInt(pageSize.toString());
  const start = (pageNum - 1) * size;
  const end = start + size;
  const paginatedData = filteredData.slice(start, end);

  return {
    success: true,
    data: {
      list: paginatedData,
      pagination: {
        current: pageNum,
        pageSize: size,
        total: filteredData.length,
        totalPages: Math.ceil(filteredData.length / size),
      },
    },
    message: '查询成功',
  };
});
